﻿using System;
using System.Collections.Generic;
using System.Linq;
using Backend.JYD.Model;

namespace Backend.JYD.EFDAL.DAL
{
    public class CodeInfoDAL : Backend.JYD.IDAL.ICodeInfo
	{
        public long AddCodeInfo(CodeInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                info.CreateTime = DateTime.Now;
                info.UpdateTime = DateTime.Now;
                db.CodeInfoes.Add(info);
                return db.SaveChanges();
            }
        }

        public bool UpdateCodeInfo(CodeInfo info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                info.UpdateTime = DateTime.Now;
                db.Entry(info).State = System.Data.Entity.EntityState.Modified;
                return db.SaveChanges() > 0;
            }
        }

        public CodeInfo GetCodeInfoByCodeType(string codeType, string code)
        {
            using (JydDataContext db = new JydDataContext())
            {
                return db.CodeInfoes.Where(p => p.TypeCode == codeType && p.Code == code).FirstOrDefault();
            }
        }

        public List<CodeInfo> GetCodeInfoByCodeType(string codeType)
        {
            using (JydDataContext db = new JydDataContext())
            {
                return db.CodeInfoes.Where(p => p.TypeCode == codeType).ToList();
            }
        }

        public List<CodeInfo> GetCodeInfo()
        {
            using (JydDataContext db = new JydDataContext())
            {
                return db.CodeInfoes.ToList();
            }
        }

        public long AddCodeType(CodeType info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                info.CreateTime = DateTime.Now;
                info.UpdateTime = DateTime.Now;
                db.CodeTypes.Add(info);
                return db.SaveChanges();
            }
        }

        public bool UpdateCodeType(CodeType info)
        {
            using (JydDataContext db = new JydDataContext())
            {
                info.UpdateTime = DateTime.Now;
                db.Entry(info).State = System.Data.Entity.EntityState.Modified;
                return db.SaveChanges() > 0;
            }
        }

        public CodeType GetCodeTypeByCodeType(string codeType)
        {
            using (JydDataContext db = new JydDataContext())
            {
                return db.CodeTypes.Where(p => p.TypeCode == codeType).FirstOrDefault();
            }
        }

        public List<CodeType> GetCodeType()
        {
            using (JydDataContext db = new JydDataContext())
            {
                return db.CodeTypes.ToList();
            }
        }
    }
}